package com.liu.sunnyday.service.impl;

import com.alibaba.dubbo.config.annotation.Service;


import com.liu.sunnyday.dao.MemberDao;
import com.liu.sunnyday.dao.OrderDao;
import com.liu.sunnyday.dao.OrderSettingDao;
import com.liu.sunnyday.exception.SunnydayException;
import com.liu.sunnyday.pojo.Member;
import com.liu.sunnyday.pojo.Order;
import com.liu.sunnyday.pojo.OrderSetting;
import com.liu.sunnyday.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * chance！Time
 *
 * @author liuqian
 * @date 2022-06-30 15:18
 * *
 */
@Service(interfaceClass = OrderService.class)
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderSettingDao orderSettingDao;
    @Autowired
    private MemberDao memberDao;
    @Autowired
    private OrderDao orderDao;

    @Override
    @Transactional
    public Order submitOrder(Map<String, String> paraMap) throws SunnydayException{
        //1，先获取到前台预约的时间
        String orderDateStr = paraMap.get("orderDate");
        //1.1,日期转换类型
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date orderDate = null;
        try {
            //将拿到的时间根据模板转换放入orderDate
            orderDate = sdf.parse(orderDateStr);
        } catch (ParseException e) {
            throw new SunnydayException("日期格式错误");
        }

        //2,查询该日期是否存在可预约
        OrderSetting orderSetting = orderSettingDao.findByOrderDate(orderDate);
        if (null == orderSetting) {
            throw new SunnydayException("所选日期,不能预约");
        }

        //3,否则当天可以预约，判断是否约满
        if (orderSetting.getReservations() >= orderSetting.getNumber()) {
            //    如果已预约人数》=可预约人数则，不可预约
            throw new SunnydayException("抱歉，当天已约满");
        }
        //4,有预约名额，然后判断该号码是否有注册
        String telephone = paraMap.get("telephone");
        //根据电话去查询会员表
        Member member = memberDao.findByTelephone(telephone);
        if (null == member) {
            //    说明还没有注册
            //    给默认值自动帮他注册
            member = new Member();
            member.setIdCard(paraMap.get("idCard"));
            member.setName(paraMap.get("name"));
            member.setPhoneNumber(telephone);
            member.setRegTime(new Date());//给当前时间
            member.setSex(paraMap.get("sex"));

            //    注册用户
            memberDao.add(member);
        }

        //如果以注册,判断是否有重复的订单
        //根据member_id,orderDate, setmeal_id作为条件去查询t_order表，是否重复预约
        //套餐id,用户id,和订单时间来确定
        Order order = new Order();
        order.setMemberId(member.getId());//会员id
        order.setOrderDate(orderDate);//预约时间
        order.setSetmealId(Integer.valueOf(paraMap.get("setmealId")));//套餐id
        //去订单表查询是否有符合的订单(有则重复)
        List<Order> orderList = orderDao.findByCondition(order);
        //如果有重复订单
        if (null != orderList && orderList.size() > 0) {
            //    有重复订单
            throw new SunnydayException("当天以有预约");
        }
        //补充订单信息,如什么支付
        order.setOrderType(Order.ORDERTYPE_WEIXIN);
        order.setOrderStatus(Order.ORDERSTATUS_NO);
        //没有重复,添加订单
        orderDao.add(order);
        //10,更新预约数量
        orderSettingDao.editReservationsByOrderDate(orderSetting);
        return order;
    }

    @Override
    public Map<String, Object> findOrderDetailById(Integer id) {
        return orderDao.findById(id);
    }
}
